package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CPersonnels;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUserRoleConn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TCompete;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICPersonnelsService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import java.util.UUID;

@Component
public class PersonnelBu {
    @Autowired
    private ICPersonnelsService icPersonnelsService;

    //查询现场用户
    public SzResult getPersonnel(String name, String plantId, String page, String pageSize) throws Exception {
        Map map = new HashMap<>();
        Page<CPersonnels> page1 = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));
        QueryWrapper<CPersonnels> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select("PER_ID", "PER_NAME");
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PER_DELETED", 0);
        queryWrapper.like(name != null && !name.isEmpty(), "PER_NAME", name);
        Page<CPersonnels> cPersonnelList = icPersonnelsService.page(page1, queryWrapper);
        List<CPersonnels> records = cPersonnelList.getRecords();
        map.put("total", page1.getTotal());
        map.put("list", records);
        return SzResult.success(map);
    }

    //新增现场用户
    public SzResult addPersonnel(String name, String pwd, String plantId) throws Exception {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        if (name == null || name.isEmpty()) {
            return SzResult.Error("用户名不能为空");
        }
        if (pwd == null || pwd.isEmpty()) {
            return SzResult.error("密码不能为空");
        }
        QueryWrapper<CPersonnels> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PER_NAME", name);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PER_DELETED", 0);
        CPersonnels cPersonnels1 = icPersonnelsService.getOne(queryWrapper);
        if (cPersonnels1 != null) {
            return SzResult.Error("该用户已存在");
        }
        CPersonnels cPersonnels = new CPersonnels();
        cPersonnels.setId(uuid);
        cPersonnels.setPerName(name);
        cPersonnels.setDataFrom(plantId);
        cPersonnels.setPerDeleted(0);
        cPersonnels.setPerPwd(pwd);
        QueryWrapper<CPersonnels> personnelsQueryWrapper = new QueryWrapper<>();
        personnelsQueryWrapper.orderByDesc("PDT_PLANEX_ID");
        personnelsQueryWrapper.last("limit 1");
        personnelsQueryWrapper.eq("DATA_FROM", plantId);
        CPersonnels personnels = icPersonnelsService.getOne(personnelsQueryWrapper);
        if (personnels != null) {
            BigDecimal perId = personnels.getPerId();
            String perId1 = String.valueOf((perId.intValue() + 1));
            cPersonnels.setPerId(new BigDecimal(perId1));
        } else {
            cPersonnels.setPerId(new BigDecimal(1));
        }
        icPersonnelsService.save(cPersonnels);
        return SzResult.Success();
    }

    //修改现场用户
    public SzResult updatePersonnel(String name, String pwd, String plantId, String id) throws Exception {
        QueryWrapper<CPersonnels> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PER_NAME", name);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PER_DELETED", 0);
        queryWrapper.ne("ID", id);
        CPersonnels cPersonnels1 = icPersonnelsService.getOne(queryWrapper);
        if (cPersonnels1 != null) {
            return SzResult.Error("该用户已存在");
        }

        QueryWrapper<CPersonnels> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ID", id);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.eq("PER_DELETED", 0);
        CPersonnels cPersonnels2 = icPersonnelsService.getOne(queryWrapper1);
        cPersonnels2.setPerName(name);
        cPersonnels2.setPerPwd(pwd);
        icPersonnelsService.updateById(cPersonnels2);
        return SzResult.Success();
    }

    //删除现场用户
    public SzResult deletePersonnel(String id, String plantId) throws Exception {
        QueryWrapper<CPersonnels> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("PER_DELETED", 0);
        CPersonnels cPersonnels = icPersonnelsService.getOne(queryWrapper);
        cPersonnels.setPerDeleted(1);
        icPersonnelsService.updateById(cPersonnels);
        return SzResult.Success();
    }
}